package day02;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 求整数平均值
 * @Author: ZYX
 * @Date: 2022/2/9 16:48
 * @Version: 1.0
 */
public class Demo06 {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env.addSource(new SourceFunction<Integer>() {
            private boolean running = true ;
            private Random random = new Random();

            @Override
            public void run(SourceContext<Integer> sourceContext) throws Exception {
                while (running) {
                    sourceContext.collect(random.nextInt(10));
                    TimeUnit.SECONDS.sleep(1L);
                }
            }

            @Override
            public void cancel() {
                running = false;
            }
        }).map(obj -> Tuple2.of(obj,1))
                .returns(Types.TUPLE(Types.INT,Types.INT))
                .keyBy(obj -> true)
                .reduce(new ReduceFunction<Tuple2<Integer, Integer>>() {
                    @Override
                    public Tuple2<Integer, Integer> reduce(Tuple2<Integer, Integer> value1, Tuple2<Integer, Integer> value2) throws Exception {
                        return Tuple2.of(value1.f0+value2.f0,value1.f1+value2.f1);
                    }
                }).map(new MapFunction<Tuple2<Integer, Integer>, Double>() {
            @Override
            public Double map(Tuple2<Integer, Integer> value) throws Exception {
                return Double.valueOf(value.f0/value.f1);
            }
        }).print();

        env.execute();
    }

}
